package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.vo.ContractProductVo;
import cn.itcast.vo.ResultInfo;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/cargo/invoice")
@Log4j
public class InvoiceController extends BaseController {

    @Reference
    private InvoiceService invoiceService;


    @RequestMapping("/list.do")
    public String list(Model md,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize){

        PageInfo<Invoice> pageInfo =
                invoiceService.findByPage(new InvoiceExample(), pageNum, pageSize);
        md.addAttribute("pageInfo",pageInfo);
        return "cargo/invoice/invoice-list";
    }

    /**
     * 通过hidden input ==>  invoice id
     * @param id 委托单号
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(String id,Model md){
        md.addAttribute("invoiceId",id);
        return "cargo/invoice/invoice-add";
    }


    /**
     * 跳转
     * @param invoiceId
     * @return
     */
    @RequestMapping("/toView")
    public String toView(String invoiceId,Model md){
        Invoice invoice = invoiceService.findById(invoiceId);
        md.addAttribute("invoice",invoice);
        return "cargo/invoice/invoice-view";
    }

    /**
     *
     * @param invoice
     * @return
     */
    @RequestMapping("/edit.do")
    public String edit(Invoice invoice, MultipartFile pickupPhoto){
        User user = getLoginUser();

        //公用主键，一定有id才能创建

        invoice.setCreateBy(user.getId());
        invoice.setCreateDepet(user.getDeptId());
        invoice.setCreateTime(new Date());


        if(pickupPhoto.getSize()==0){
            invoice.setPickupPhoto(null);
        }else {
            String filename = pickupPhoto.getOriginalFilename();
            invoice.setPickupPhoto(filename);
        }


        invoiceService.save(invoice);
        return "cargo/invoice/invoice-list";
    }


    /**
     * delete invoice
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResultInfo delete(String id){
        return invoiceService.deleteById(id);
    }

    /**
     * 导出Excel
     * @param invoiceId
     * @throws IOException
     */
    @RequestMapping("/toExcel.do")
    @ResponseBody
    public void printExcel_22(String invoiceId) throws IOException {
        // 【获取excel文件流，传入工作簿】
        InputStream in = session.getServletContext()
                .getResourceAsStream("make/xlsprint/printExcel.xlsx");
        Workbook workbook = new XSSFWorkbook(in);

        //3. 导出数据行
        Invoice invoice = invoiceService.findById(invoiceId);
        // 【获取工作表】
        Sheet sheet = workbook.getSheetAt(0);

        // 【获取第一行】
        Row row = sheet.getRow(2);

        Cell cell = row.createCell(1);
        cell.setCellValue(invoice.getInvoiceId());
        cell.setCellStyle(this.text(workbook));

        cell = row.createCell(2);
        cell.setCellValue(invoice.getScNo());
        cell.setCellStyle(this.text(workbook));

        cell = row.createCell(3);
        cell.setCellValue(invoice.getTradeTerms());
        cell.setCellStyle(this.text(workbook));

        cell = row.createCell(4);
        cell.setCellValue(invoice.getInvoiceMoney());
        cell.setCellStyle(this.text(workbook));

        cell = row.createCell(5);
        cell.setCellValue(invoice.getInvoiceTime());
        cell.setCellStyle(this.text(workbook));

        cell = row.createCell(6);
        String state = invoice.getState()==0?"草稿" :"已生成财务单";
        cell.setCellValue(state);
        cell.setCellStyle(this.text(workbook));


        //4. 导出下载
        // 设置编码
        response.setCharacterEncoding("UTF-8");
        // 设置下载响应头
        response.setHeader("content-disposition","attachment;fileName=export.xlsx");
        // 把excel文件流，写入到哪个输出流？ response输出流
        workbook.write(response.getOutputStream());
        workbook.close();
    }



    /**
     * url:/cargo/invoice/hasCreated
     * @param id
     * @return
     */
    @RequestMapping("/hasCreated")
    @ResponseBody
    public Map<String,Integer> isCreated(String id){
        return invoiceService.hasCreated(id);
    }

    //大标题的样式
    public CellStyle bigTitle(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)16);
        font.setBold(true);//字体加粗
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);				//横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        return style;
    }

    //小标题的样式
    public CellStyle title(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short)12);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);				//横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        style.setBorderTop(BorderStyle.THIN);						//上细线
        style.setBorderBottom(BorderStyle.THIN);					//下细线
        style.setBorderLeft(BorderStyle.THIN);						//左细线
        style.setBorderRight(BorderStyle.THIN);						//右细线
        return style;
    }

    //文字样式
    public CellStyle text(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)10);

        style.setFont(font);

        style.setAlignment(HorizontalAlignment.LEFT);				//横向居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        style.setBorderTop(BorderStyle.THIN);						//上细线
        style.setBorderBottom(BorderStyle.THIN);					//下细线
        style.setBorderLeft(BorderStyle.THIN);						//左细线
        style.setBorderRight(BorderStyle.THIN);						//右细线

        return style;
    }






}
